package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class UseSealSearchService extends BaseService {

	@Task(code = "353", tip = "用印自查(@cnt)")
	public long selectUseSealLogList() {

		String sql = "select count(*) cnt from t_seal_use_apply t "
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and t.verify_status='待审核' ";
		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());

		return cnt;
	}
	
	@Transactional
	public List<Map<String, Object>> selectUseSealLogList(String srh_reason,
			String srh_name, Page page) {

		String sql = "select t.*,tu.uname,ts.name,ts.true_code from t_seal_use_apply t "
				+ " left join t_user tu on t.apply_user=tu._id "
				+ " left join t_seal ts on t.seal_id=ts._id "
				+ " where t.status=0 and t.bank_id=? and t.seal_status='已盖章' "
				+ " and t.verify_status='待审核' and t.reason like ? "
				+ " and (ts.name like ? or ts.true_code like ?) order by t.created desc ";
		List<String> params = new ArrayList<String>();

		params.add(WebTools.getCurrentBankId());
		if (ChkTools.isNull(srh_reason)) {
			sql = sql.replace("and t.reason like ?", "");
		} else {
			params.add("%" + srh_reason + "%");
		}
		if (ChkTools.isNull(srh_name)) {
			sql = sql
					.replace("and (ts.name like ? or ts.true_code like ?)", "");
		} else {
			params.add("%" + srh_name + "%");
			params.add("%" + srh_name + "%");
		}

		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	// 自查历史
	public List<Map<String, Object>> selectSearchLogList(String srh_content,
			String srh_start, String srh_end, String srh_user, Page page) {
		String sql = "select t.*,tu.uname,ta.reason,ts.name,ts.true_code from t_use_search_log t "
				+ " left join t_user tu on t.user_id=tu._id "
				+ " left join t_seal_use_apply ta on t.apply_id=ta._id "
				+ " left join t_seal ts on ta.seal_id=ts._id "
				+ " where t.status=0 and t.bank_id=? and tu.uname like ? and t.search_content like ? "
				+ " and t.search_time>=? and t.search_time<=? and t.search_type='self search' order by t.created desc ";

		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());
		if (ChkTools.isNull(srh_user)) {
			sql = sql.replace("and tu.uname like ?", "");
		} else {
			params.add("%" + srh_user + "%");
		}
		if (ChkTools.isNull(srh_content)) {
			sql = sql.replace("and t.search_content like ?", "");
		} else {
			params.add("%" + srh_content + "%");
		}
		if (ChkTools.isNull(srh_start)) {
			sql = sql.replace("and t.search_time>=? and t.search_time<=?", "");
		} else {
			params.add(srh_start);
			params.add(DateTools.addOneDay(srh_end));
		}
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	public List<Map<String, Object>> selectUseApplyPpList(String apply_id) {
		String sql = "select t.* from t_seal_use_apply_pp t "
				+ " where t.status=0 and t.apply_id=? order by t.created desc";
		List<Map<String, Object>> list = jt.queryForList(sql, apply_id);
		return list;
	}
}
